package face.pay.web.com.cenenter.userdata.controller;


import face.pay.system.annotation.CurrentUser;
import face.pay.entity.UserInfo;
import face.pay.util.ResultMessage;

import face.pay.web.com.user.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(value = "/user_center")
@Slf4j
public class UserDataController {

    @Autowired
    IUserService iUserService;

    /**
     * 用户资料查询
     * @param loginUser
     * @param model
     * @return
     */
    @RequestMapping(value = "/to_user_profile", method = RequestMethod.GET)
    public String myData(@CurrentUser UserInfo loginUser, Model model) {
        //判断当前用户是否登录
        if(null==loginUser){
            return "login";
        }
        loginUser.setDeleted(0);
        UserInfo userdata=iUserService.queryUserInfo(loginUser);
        String mobile=userdata.getMobile();

        model.addAttribute("userInfo",userdata);
        return "com/center/userData";
    }

    /**
     * 更新用户信息
     * @param userInfo
     * @return
     */
    @PostMapping("/updateUserInfo")
    @ResponseBody
    public ResultMessage updateUserInfo(UserInfo userInfo, String code , @CurrentUser UserInfo loginUser){
        //获取当前用户的id
        if(!loginUser.getId().equals(userInfo.getId())){
            //前端id和用户id不一样,被人串改不能修改信息
            log.info("用户登录信息被劫持:"+loginUser.getUserName());
            return ResultMessage.createFail("修改失败");
        }
        userInfo.setUpdatedBy(loginUser.getId());
        return iUserService.updateUserInfo(userInfo,code,loginUser.getId());

    }


}
